/*
 * Notes:
 * 1. use SEQUENCE strategy for the id generating, ok or not?
 * 2. to-do: relationship field
 * 3. nric - 1) how to set it as unique? 2)check validity?
 * 4. password - I set as 12-digit random alpha-numeric password for new account.
 */
package entity;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.*;

/**
 *
 * @author Ye Mei
 */
@Entity(name="SystemUser")
@Inheritance(strategy=InheritanceType.JOINED)
public class SystemUserEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private String id;
    private String nric;
    private String password;
    private String name;
    private int age;
    private String gender;
    private String dob;
    private String contactNumber;
    private String email;
    private String hobby;
    private String preference;
    private String dateAdded;
        
    public SystemUserEntity(){        
    }
    
    public void create(String nric, String name, int age, String gender, String dob, 
            String contactNumber,String email, String hobby, String preference){
        this.setNric(nric);
        this.setName(name);
        this.setAge(age);
        this.setGender(gender);
        this.setDob(dob);
        this.setContactNumber(contactNumber);
        this.setEmail(email);
        this.setHobby(hobby);
        this.setPreference(preference);
        this.setDateAdded();
        
        // generate a random 12-digit alpha-numeric password for a new account
        SecureRandom random = new SecureRandom();
        int length = 12;
        this.password = new BigInteger(130, random).toString(length);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }       
 
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getNric() {
        return nric;
    }

    public void setNric(String nric) {
        this.nric = nric;
    }

    public String getPreference() {
        return preference;
    }

    public void setPreference(String preference) {
        this.preference = preference;
    }

    public String getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        this.dateAdded = dateFormat.format(new Date());
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SystemUserEntity)) {
            return false;
        }
        SystemUserEntity other = (SystemUserEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.SystemUserEntity[ id=" + id + " ]";
    }
    
}
